package com.hmsproject.db.util;

import java.util.ArrayList;
import java.util.List;

import com.hmsproject.datastructures.HotelImage;
import com.hmsproject.datastructures.Hotel;
import com.hmsproject.datastructures.StarValue;
import com.hmsproject.datastructures.User;
import com.hmsproject.datastructures.enums.FacilitiesEnum;
import com.hmsproject.datastructures.enums.HotelSizeEnum;
import com.hmsproject.db.HMSDBConnectionFactory;
import com.hmsproject.db.HMSDBType;
import com.mymobile.showcase.mobile.ForgotPasswordView;
import com.mymobile.showcase.mobile.SignInUserView;
import com.mymobile.showcase.mobile.SignUpUserView;

public class HMSDBUtil {
	
	private static HMSDBUtil hmsDBUtil = new HMSDBUtil();

	private HMSDBUtil(){}
	
	public static HMSDBUtil getInstance(){
		return hmsDBUtil;
	}
	
	public void updateUser(User user){
		try{
			HMSDBConnectionFactory.getInstance().getDBConnection(HMSDBType.INMEMORYDB).updateUser(user);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public User getUser(String emailAddress){

			return HMSDBConnectionFactory.getInstance().getDBConnection(HMSDBType.INMEMORYDB).getUser(emailAddress);
	}
	
	public boolean changePassword(String emailAddress,String newPassword)
	{
		return HMSDBConnectionFactory.getInstance().getDBConnection(HMSDBType.INMEMORYDB).changePassword(emailAddress, newPassword);
	}		
	
	
	public String forgotPassword(ForgotPasswordView forgotPasswordView) throws Exception{
		
		String password = HMSDBConnectionFactory.getInstance().getDBConnection(HMSDBType.INMEMORYDB).forgotPassword(forgotPasswordView);
		
		return password;
	}	
	
	public boolean signInUser(SignInUserView signInUserView) {
		
		boolean loginSuccessful = HMSDBConnectionFactory.getInstance().getDBConnection(HMSDBType.INMEMORYDB).signInUser(signInUserView);
		
		return loginSuccessful;
	}	
		
	
	public boolean verifyUser(String verificationStr) throws Exception{
		
		boolean result = false;
			
		if(verificationStr == null || verificationStr.length() == 0){
			throw new Exception("Invalid verification String, please check");
		}
		
		result = HMSDBConnectionFactory.getInstance().getDBConnection(HMSDBType.INMEMORYDB).verifyUser(verificationStr);
		
		return result;
	}	
	
	public long signUpUser(SignUpUserView signUpUserView) throws Exception{
		
		long verification_code = HMSDBConnectionFactory.getInstance().getDBConnection(HMSDBType.INMEMORYDB).signUpUser(signUpUserView);
		
		return verification_code;
	}		
	
	private static HotelSizeEnum getHotelSize(String hotelSize){
		HotelSizeEnum hotelSizeEnum = HotelSizeEnum.FIFTY;
		if(hotelSize.equals("100")){
			hotelSizeEnum = HotelSizeEnum.FIFTY_ONE_FIFTY;
		}else
		if(hotelSize.equals("150")){
			hotelSizeEnum = HotelSizeEnum.MORE_THAN_ONE_FIFTY;
		}		
		
		return hotelSizeEnum;
	}
	
	private static HotelImage getHotelImage(String filePath) throws Exception{

		/*File imageFile = new File(filePath);
		FileInputStream f = new FileInputStream(imageFile);
		byte b[] = new byte[f.available()];
		f.read(b);
		Binary imageData = new Binary(b);*/
		HotelImage hotelImage = new HotelImage(filePath, null);		
		
		return hotelImage;
		
	}
	
	private static List<FacilitiesEnum> getFacilitiesList(String spa,String wifi,
				String breakfast,String pool,String beach) {
		
		List<FacilitiesEnum> facilitiesList = new ArrayList<FacilitiesEnum>();

		if(spa.equals("YES")){
			facilitiesList.add(FacilitiesEnum.SPA);
		}
		
		if(wifi.equals("YES")){
			facilitiesList.add(FacilitiesEnum.WIFI);
		}
		
		if(breakfast.equals("YES")){
			facilitiesList.add(FacilitiesEnum.BREAKFAST);
		}
		
		if(pool.equals("YES")){
			facilitiesList.add(FacilitiesEnum.POOL);
		}
		
		if(beach.equals("YES")){
			facilitiesList.add(FacilitiesEnum.BEACH);
		}		
		
		return facilitiesList;
		
	}	
	
	public static void main(String args[]) throws Exception{
		
		String name = args[0];
		String placeName = args[1];
		int price = Integer.parseInt(args[2]);
		int starRating = Integer.parseInt(args[3]);
		
		HotelSizeEnum hotelSizeEnum = getHotelSize(args[4]);
		HotelImage hotelImage = getHotelImage(args[5]);
		
		
		int popularity = Integer.parseInt(args[6]);
		int distance = Integer.parseInt(args[7]);
		int review = Integer.parseInt(args[8]);	
		StarValue starValue = new StarValue(popularity, distance, review);
		
		List<FacilitiesEnum> faclitiesList = getFacilitiesList(args[9],args[10],args[11],args[12],args[13]);
		
		Hotel hotels = new Hotel("0",name, placeName, price, starRating, hotelSizeEnum, hotelImage, starValue,faclitiesList);
		
		HMSDBConnectionFactory.getInstance().getDBConnection().insertHotel(hotels);
		
		System.out.println("INserted " + name);
		
		/*SearchBean searchBean = new SearchBean();
		searchBean.setHotelSorting(HotelSorting.NAME_A_TO_Z);
		searchBean.setHotelSorting(HotelSorting.DISTANCE);
		searchBean.setHotelSorting(HotelSorting.REVIEW_SCORE);
		earchBean.setHotelSorting(HotelSorting.PRICE_LOW_TO_HIGH);
		searchBean.setHotelSorting(HotelSorting.PRICE_HIGH_TO_LOW);
		searchBean.setHotelSorting(HotelSorting.STAR_FIVE_ZERO);
		List<FacilitiesEnum> facilitiesList = new ArrayList<FacilitiesEnum>();
		facilitiesList.add(FacilitiesEnum.SPA);
		facilitiesList.add(FacilitiesEnum.BREAKFAST);
		facilitiesList.add(FacilitiesEnum.POOL);
		facilitiesList.add(FacilitiesEnum.BEACH);
		facilitiesList.add(FacilitiesEnum.WIFI);
		FilterBean filterBean = new FilterBean();
		filterBean.setFacilities(facilitiesList);
		
		PriceRange priceRange = new PriceRange();
		priceRange.setLowerBound("100");
		priceRange.setUpperBound("500");
		filterBean.setPriceRange(priceRange);
		
		searchBean.setFilterBean(filterBean);	*/	

		//System.out.println(HMSDBConnectionFactory.getInstance().getDBConnection().searchHotels(searchBean));
	}	
}
